﻿using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Xml.Serialization;
using CodePlex.DnD4eCampaignManager.DnD4e;
using CodePlex.DnD4eCampaignManager.DnD4e.Interfaces;
using CodePlex.DnD4eCampaignManager.Repositories.Interfaces;

namespace CodePlex.DnD4eCampaignManager.Repositories
{
    public class LocationRepository : ILocationRepository
    {
        private const string LOCATION_PATH_AND_EXTENSION = "Location";

        public LocationRepository()
        {
            if ( !Directory.Exists(string.Format("{0}s", LOCATION_PATH_AND_EXTENSION)) )
            {
                Directory.CreateDirectory(string.Format("{0}s", LOCATION_PATH_AND_EXTENSION));
            }
        }

        public ReadOnlyCollection<string> GetAllNames()
        {
            List<string> namesFromFileSystem = new List<string>();
            foreach ( string location in Directory.GetFiles(string.Format("{0}s", LOCATION_PATH_AND_EXTENSION)) )
            {
                FileInfo fileInfo = new FileInfo(location);
                if ( fileInfo.Name.Replace(fileInfo.Extension, string.Empty) != string.Empty )
                {
                    namesFromFileSystem.Add(fileInfo.Name.Replace(fileInfo.Extension, string.Empty));
                }
            }
            return namesFromFileSystem.AsReadOnly();
        }

        public ILocation GetByName(string playerCharacterName)
        {
            if ( playerCharacterName == string.Empty )
            {
                return null;
            }
            XmlSerializer xmlSerializer = new XmlSerializer(typeof ( Location ));
            string locationPath = string.Format("{0}s\\{1}.{2}", LOCATION_PATH_AND_EXTENSION, playerCharacterName,
                                                LOCATION_PATH_AND_EXTENSION.ToLower());
            using ( TextReader textReader =
                new StreamReader(locationPath) )
            {
                Location location = xmlSerializer.Deserialize(textReader) as Location;
                if ( location != null )
                {
                    return location;
                }
            }
            return null;
        }

        public void ImportLocationImageFile(string fileName, string locationName)
        {
            // Validate the file is an image

            // Copy the image to the repository directory and save it with the location's name
        }

        public void Save(ILocation playerCharacter)
        {
            if ( playerCharacter.Index.HasValue || playerCharacter.Name == string.Empty )
            {
                return;
            }
            XmlSerializer xmlSerializer = new XmlSerializer(typeof ( Location ));
            using ( TextWriter textWriter =
                new StreamWriter(string.Format("{0}s\\{1}.{2}", LOCATION_PATH_AND_EXTENSION, playerCharacter.Name,
                                               LOCATION_PATH_AND_EXTENSION.ToLower())) )
            {
                xmlSerializer.Serialize(textWriter, playerCharacter);
            }
        }
    }
}